package cn.yuhe.system.controller;

import cn.yuhe.basic.annotation.PreAuthorize;
import cn.yuhe.basic.utils.AjaxResult;
import cn.yuhe.basic.utils.PageList;
import cn.yuhe.system.domain.DictionaryType;
import cn.yuhe.system.query.DictionaryTypeQuery;
import cn.yuhe.system.service.IDictionaryTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* @Description: 控制层
* @Author: Mr.Qiu
* @Version: 1.0
* @Date:
*/
@RestController
@RequestMapping("/dictionaryType")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DictionaryTypeController {

    @Autowired
    private IDictionaryTypeService dictionaryTypeService;

    /** 
    * @Title: 根据id查询
    * @Description: 根据id查询
    * @Author: Mr.Qiu
    * @Version: 1.0
    * @Date:  2022/6/24 10:13  
    * @Parameters: [id]
    * @Return cn.yuhe.org.domain.Department
    */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    @PreAuthorize(name = "根据id查询数据字典明细",sn = "dictionaryType:loadById")
    public DictionaryType loadById(@ApiParam(value="部门表属性",required = true) @PathVariable("id") Long id){

        return dictionaryTypeService.loadById(id);
    }

    /**
    * @Description: 查询所有
    * @Author: Mr.Qiu
    * @Version: 1.0
    * @Date:
    */
    @GetMapping
    @PreAuthorize(name = "查询所有数据字典明细",sn = "dictionaryType:loadAll")
    public List<DictionaryType> loadAll(){

        return dictionaryTypeService.loadAll();
    }

    /** 
    * @Title: 根据id删除
    * @Description: 根据id删除
    * @Author: Mr.Qiu
    * @Version: 1.0
    * @Date:  2022/6/24 11:53  
    * @Parameters: []
    * @Return java.util.List<cn.yuhe.org.domain.Department>
    */
    @DeleteMapping("/{id}")
    @PreAuthorize(name = "根据id删除数据字典明细",sn = "dictionaryType:remove")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
           dictionaryTypeService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }

    /**
    * @Title: 新增修改
    * @Description: 新增修改
    * @Author: Mr.Qiu
    * @Version: 1.0
    * @Date:  2022/6/24 12:56
    * @Parameters: [department]
    * @Return cn.yuhe.basic.utils.AjaxResult
    */
    @PutMapping
    @PreAuthorize(name = "新增修改数据字典明细",sn = "dictionaryType:addOrUpt")
    public AjaxResult addOrUpt(@RequestBody DictionaryType dictionaryType){
        try {
            if (dictionaryType.getId() == null) {
                 dictionaryTypeService.save(dictionaryType);
            } else {
                dictionaryTypeService.update(dictionaryType);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }


    @PostMapping
    @PreAuthorize(name = "高级查询数据字典明细",sn = "dictionaryType:querData")
    public PageList<DictionaryType> querData(@RequestBody DictionaryTypeQuery dictionaryTypeQuery){
            PageList<DictionaryType> data = dictionaryTypeService.queryData(dictionaryTypeQuery);
            return data;
    }

    @PatchMapping
    @PreAuthorize(name = "批量删除数据字典明细",sn = "dictionaryType:batchDel")
    public AjaxResult batchDel(@RequestBody Long[] ids){
        try {
            dictionaryTypeService.batchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }
}
